package com.ruoyi.system.service.impl;

import java.util.List;

import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.BusResourceTypeMapper;
import com.ruoyi.system.domain.BusResourceType;
import com.ruoyi.system.service.IBusResourceTypeService;
import com.ruoyi.common.core.text.Convert;

/**
 * 资源分类 服务层实现
 * 
 * @author ruoyi
 * @date 2021-01-07
 */
@Service
public class BusResourceTypeServiceImpl implements IBusResourceTypeService 
{
	@Autowired
	private BusResourceTypeMapper busResourceTypeMapper;

	/**
     * 查询资源分类信息
     * 
     * @param typeId 资源分类ID
     * @return 资源分类信息
     */
    @Override
	public BusResourceType selectBusResourceTypeById(Long typeId)
	{
	    return busResourceTypeMapper.selectBusResourceTypeById(typeId);
	}
	
	/**
     * 查询资源分类列表
     * 
     * @param busResourceType 资源分类信息
     * @return 资源分类集合
     */
	@Override
	public List<BusResourceType> selectBusResourceTypeList(BusResourceType busResourceType)
	{
	    return busResourceTypeMapper.selectBusResourceTypeList(busResourceType);
	}
	
    /**
     * 新增资源分类
     * 
     * @param busResourceType 资源分类信息
     * @return 结果
     */
	@Override
	public int insertBusResourceType(BusResourceType busResourceType)
	{
		this.bulidResourceType(busResourceType);
	    return busResourceTypeMapper.insertBusResourceType(busResourceType);
	}
	
	/**
     * 修改资源分类
     * 
     * @param busResourceType 资源分类信息
     * @return 结果
     */
	@Override
	public int updateBusResourceType(BusResourceType busResourceType)
	{
		this.bulidResourceType(busResourceType);
	    return busResourceTypeMapper.updateBusResourceType(busResourceType);
	}

	/**
     * 删除资源分类对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
	@Override
	public int deleteBusResourceTypeByIds(String ids)
	{
		return busResourceTypeMapper.deleteBusResourceTypeByIds(Convert.toStrArray(ids));
	}

	/**
	 * 校验分类名称是否唯一
	 *
	 * @param busResourceType 分类信息
	 * @return 结果
	 */
	@Override
	public String checkTypeNameUnique(BusResourceType busResourceType)
	{
		Long typeId = StringUtils.isNull(busResourceType.getTypeId()) ? -1L : busResourceType.getTypeId();
		BusResourceType info = busResourceTypeMapper.checkTypeNameUnique(busResourceType.getTypeName(), busResourceType.getParentId());
		if (StringUtils.isNotNull(info) && info.getTypeId().longValue() != typeId.longValue())
		{
			return "1";
		}
		return "0";
	}



	/**
	 * 构建资源分类对象
	 * @param busResourceType
	 */
	private void bulidResourceType(BusResourceType busResourceType){
		if(busResourceType.getParentId() != null && busResourceType.getParentId() != 0L){
			BusResourceType parentType = busResourceTypeMapper.selectBusResourceTypeById(busResourceType.getParentId());
			if(parentType != null){
				busResourceType.setParentIds(parentType.getParentIds()+","+parentType.getTypeId());
				if(parentType.getParentNames()==null || parentType.getParentNames().equals("")){
					busResourceType.setParentNames(parentType.getTypeName());
				}else{
					busResourceType.setParentNames(parentType.getParentNames()+","+parentType.getTypeName());
				}
			}else{
				busResourceType.setParentId(0L);
				busResourceType.setParentIds("0");
				busResourceType.setParentNames("");
			}
		}else{
			busResourceType.setParentId(0L);
			busResourceType.setParentIds("0");
			busResourceType.setParentNames("");
		}
	}
	
}
